#include "stm32f10x.h"
#include "lcd.h"
#include "key.h"
#include "start.h"
#include "rtc.h"
#include "usart.h"


Datadef data[100];
u8 num = 0;
u8 limit_state = 0;
u8 wet_temp = 0;
u8 over_flag = 0xA3;
extern u8 LCD_state;
extern u8 led3_flag;
void delay_ms(u32 nTime);

//Main Body 
int main(void)
{
	char txt[20];
	SysTick_Config(SystemCoreClock/1000);
	STM3210B_LCD_Init();
	LCD_SetTextColor(White);
	LCD_SetBackColor(Blue2);
	LCD_Clear(Blue2);
	IO_Init();
	i2c_init();
	key_Init();
	rtc_Init();
	usart_Init();
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);

	while(1)
	{
		led_ctrl(led3,led3_flag);
		state_switch();
		switch(LCD_state){
		case LCD_state_showing:{
			sprintf(txt,"       Data");
			LCD_DisplayStringLine(Line2, txt);
			sprintf(txt,"Temperature: %dC     ",data[num].temper);
			LCD_DisplayStringLine(Line4, txt);
			sprintf(txt,"Hum: %d%%          ",data[num].wet);
			LCD_DisplayStringLine(Line5, txt);
			sprintf(txt,"time: %0.2d:%d:%0.2d      ",RTC_GetCounter()/3600,
																	(RTC_GetCounter()%3600)/60,
																	RTC_GetCounter()%60);
			LCD_DisplayStringLine(Line6, txt);
			sprintf(txt,"Mark times: %d         ",num);
			LCD_DisplayStringLine(Line7, txt);
		}break;
		case LCD_state_setting_temperUp:{
			sprintf(txt,"     DataSetting       ");
			LCD_DisplayStringLine(Line2, txt);
			
			LCD_SetTextColor(Red);
			sprintf(txt,"TemperatureLimit: %d       ",data_set.temper_limit);
			LCD_DisplayStringLine(Line4, txt);
			LCD_SetTextColor(White);
			
			sprintf(txt,"Humlimit: %d     ",data_set.wet_limit);
			LCD_DisplayStringLine(Line5, txt);
			sprintf(txt,"CatchOffTime: %ds     ",data_set.catch_off);
			LCD_DisplayStringLine(Line6, txt);
			sprintf(txt,"TestSignal: %.1fKHz     ",data_set.test_signal);
			LCD_DisplayStringLine(Line7, txt);
		}break;
		case LCD_state_setting_wetUp:{
			sprintf(txt,"    DataSetting     ");
			LCD_DisplayStringLine(Line2, txt);
			sprintf(txt,"TemperatureLimit: %d     ",data_set.temper_limit);
			LCD_DisplayStringLine(Line4, txt);
			
			LCD_SetTextColor(Red);
			sprintf(txt,"Humlimit: %d"     ,data_set.wet_limit);
			LCD_DisplayStringLine(Line5, txt);
			LCD_SetTextColor(White);
			
			sprintf(txt,"CatchOffTime: %ds     ",data_set.catch_off);
			LCD_DisplayStringLine(Line6, txt);
			sprintf(txt,"TestSignal: %.1fKHz     ",data_set.test_signal);
			LCD_DisplayStringLine(Line7, txt);
		}break;
		case LCD_state_setting_off:{
			sprintf(txt,"    DataSetting     ");
			LCD_DisplayStringLine(Line2, txt);
			sprintf(txt,"TemperatureLimit: %d     ",data_set.temper_limit);
			LCD_DisplayStringLine(Line4, txt);
			sprintf(txt,"Humlimit: %d          ",data_set.wet_limit);
			LCD_DisplayStringLine(Line5, txt);
			
			LCD_SetTextColor(Red);
			sprintf(txt,"CatchOffTime: %ds     ",data_set.catch_off);
			LCD_DisplayStringLine(Line6, txt);
			LCD_SetTextColor(White);
			
			sprintf(txt,"TestSignal: %.1fKHz     ",data_set.test_signal);
			LCD_DisplayStringLine(Line7, txt);
		}break;
		case LCD_state_setting_signal:{
			sprintf(txt,"    DataSetting     ");
			LCD_DisplayStringLine(Line2, txt);
			sprintf(txt,"TemperatureLimit: %d     ",data_set.temper_limit);
			LCD_DisplayStringLine(Line4, txt);
			sprintf(txt,"Humlimit: %d     ",data_set.wet_limit);
			LCD_DisplayStringLine(Line5, txt);
			
			sprintf(txt,"CatchOffTime: %ds     ",data_set.catch_off);
			LCD_DisplayStringLine(Line6, txt);
			
			LCD_SetTextColor(Red);
			sprintf(txt,"TestSignal: %.1fKHz     ",data_set.test_signal);
			LCD_DisplayStringLine(Line7, txt);
			LCD_SetTextColor(White);
		}break;
	}
	}
}

